package net.diandangquan.pipeline;

import net.diandangquan.doc.XBiaoData;
import net.diandangquan.repository.XBiaoSpiderRepository;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Map;

/**
 * 腕表之家相关的存储处理
 *
 * @author Tiger
 * @date 2017/12/06 18:25
 */
@Component
public class XBiaoPipeLine implements Pipeline {

    @Autowired
    private XBiaoSpiderRepository spiderRepository;

    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String, Object> result = resultItems.getAll();
        String xbiaoId = String.valueOf(result.get("xbiaoId"));
        XBiaoData xBiaoData = spiderRepository.findByXbiaoId(xbiaoId);
        if(xBiaoData==null){
            xBiaoData = new XBiaoData();
        }
        try {
            BeanUtils.populate(xBiaoData, result);
            spiderRepository.save(xBiaoData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}